import * as NSBase from '../../Common/NetServiceBase';
import { Config } from '../../Config';
import { Random } from '../../TYLibrary/Common/GlobalExtend';
import { Wx_Page } from '../EntityBase';
import { PageType } from '../Entitys';

//NS.GetOpenId
//NS.QueryIsAgree

export class LoadingBasePage extends Wx_Page {
    /**
     * 引导页最少显示时间（秒）
     */
    private m_Showtime: number = 3;

    //加载页图片
    public loadimgs: Array<string> = ['/img/loading01.jpg', '/img/loading02.jpg'];
    /**
     * 首页
     */
    public IndexPageType: PageType = PageType.AppIndex;

    public data = {
        showimg: this.loadimgs[Random(0, this.loadimgs.length)]
    }

    private _Movetimes(): void {
        let r = setTimeout(() => {
            this.m_Showtime = 0;    
        }, this.m_Showtime * 1000);
    }

    /**
     * 初始化页面信息
     * 
     * @memberof indexPage
     */
    public async InitData(options: any) {
        this._Movetimes();
        try {
            wx.showNavigationBarLoading();
            wx.setNavigationBarTitle({ title: "检查用户协议信息..." });
          
            this.App.ModuleManager.AuthManager.TYLoginUrl = Config.TYLogin;  
            await this.App.ModuleManager.Init();
            await this.OnAppInitReady();
            await this.ShowIndex();
        } catch (e) {
            this.ShowError(e);
        } finally {
            wx.setNavigationBarTitle({ title: "" });
            wx.hideNavigationBarLoading();
        }
    }

    /**
     * APP初始化完成
     *
     * @memberof indexPage
     */
    public async OnAppInitReady() { }

    public async ShowIndex(): Promise<void> {
        return new Promise<void>((resolve, reject) => {
            let r = setInterval(() => {
                if (this.m_Showtime <= 0) {
                    clearInterval(r);
                    wx.switchTab({ url: this.IndexPageType });
                    resolve();
                }
            }, 1000);
        });
    }
}
